package com.pagoamigo.domain;

import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@RooJavaBean
public class MyUserDetailsService implements UserDetailsService{
    

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<Account> accountList =
                Account.findAccountsByUsernameEquals(username).getResultList();
        final Account account = accountList.isEmpty() ? null : (Account)accountList.get(0);
        if (account == null) {
        	if (username.equals("admin")){
        		return new UserDetails() {
					
					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					public boolean isEnabled() {
						return true;
					}
					
					@Override
					public boolean isCredentialsNonExpired() {
						return true;
					}
					
					@Override
					public boolean isAccountNonLocked() {
						return true;
					}
					
					@Override
					public boolean isAccountNonExpired() {
						return true;
					}
					
					@Override
					public String getUsername() {
						return "admin";
					}
					
					@Override
					public String getPassword() {
						return "admin";
					}
					
					@Override
					public Collection<? extends GrantedAuthority> getAuthorities() {
						List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);
						authList.add(new GrantedAuthority() {
							/**
							 * 
							 */
							private static final long serialVersionUID = 1L;

							@Override
							public String getAuthority() {
								return "ROLE_ADMIN";
							}
						});
						return authList;
					}
				};
        	}
            throw new UsernameNotFoundException("User not registered: " + username);
        } else {
        	UserDetails ud = new UserDetails() {
				
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public boolean isEnabled() {
					return true;
				}
				
				@Override
				public boolean isCredentialsNonExpired() {
					return true;
				}
				
				@Override
				public boolean isAccountNonLocked() {
					return true;
				}
				
				@Override
				public boolean isAccountNonExpired() {
					return true;
				}
				
				@Override
				public String getUsername() {
					return account.getUsername();
				}
				
				@Override
				public String getPassword() {
					return account.getPassword();
				}
				
				@Override
				public Collection<? extends GrantedAuthority> getAuthorities() {
					List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);
					if (account.getAccounttype() == AccountTypes.Personal){
						authList.add(new GrantedAuthority() {
							
							/**
							 * 
							 */
							private static final long serialVersionUID = 1L;

							@Override
							public String getAuthority() {
								return "ROLE_USER";
							}
						});
					}else if (account.getAccounttype() == AccountTypes.Empresarial){
authList.add(new GrantedAuthority() {
							
							/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

							@Override
							public String getAuthority() {
								return "ROLE_ENTERPRISE";
							}
						});
					}
			        return authList;
				}
			};
            /*if (!employee .isEnabled()) {
                throw new DisabledException("User is disabled");
            }*/
            return ud;
        }
    }

}
